<?php

namespace app\models;

use think\Db;
use think\Model;

class Order extends Model
{
    protected $pk = 'id';
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'created';
    protected $defaultSoftDelete = 0;


    //商城订单操作公共方法
    /*
    $orde = 订单
    ￥status 改变状态
    ￥payment 支付方式
    $type 操作人类型 1 商家 2系统(合伙人)
    ￥aid 商家|合伙人ID
    */
    public function order_status($order = [], $status = 0, $type = 0, $aid = 0)
    {
        switch ($status) {
                //订单待付款状态变成已付款状态
            case '2':
                if ($order['status'] != 1) {
                    throw new \Exception("订单状态必须为待付款状态", 100);
                }
                //线上支付或线下支付
                if ($order['payment'] == 1) {
                    $this->where('id', $order['id'])->update(['status' => $status]);
                } else {
                    $this->where('id', $order['id'])->update(['status' => $status, 'paytime' => date('Y-m-d H:i:s'), 'ispay' => 2]);
                }
                break;
                //订单配送
            case '3':
                if ($order['status'] != 2) {
                    throw new \Exception("订单状态必须为待配送状态", 100);
                }
                //订单完成操作
                if ($order['payment'] == 1) {
                    $this->where('id', $order['id'])->update(['status' => $status, 'type' => $type, 'aid' => $aid, 'distime' => date('Y-m-d H:i:s')]);
                } else {
                    $this->where('id', $order['id'])->update(['status' => $status, 'type' => $type, 'aid' => $aid, 'distime' => date('Y-m-d H:i:s')]);
                }
                break;
                //订单完成
            case '4':
                if ($order['status'] != 3) {
                    throw new \Exception("订单状态必须为待完成状态", 100);
                }
                //订单完成操作
                if ($order['payment'] == 1) {
                    $this->where('id', $order['id'])->update(['status' => $status, 'ispay' => 2, 'distime' => date('Y-m-d H:i:s'), 'fintime' => date('Y-m-d H:i:s')]);
                } else {
                    $this->where('id', $order['id'])->update(['status' => $status, 'fintime' => date('Y-m-d H:i:s')]);
                }
                //减少库存增加销量
                $ordergoods = (new OrderGoods())->where('ordersn', $order['ordersn'])->select();
                foreach ($ordergoods as $goods) {
                    (new OrderGoods())->where('id', $goods['id'])->update(['status' => $status, 'fintime' => date('Y-m-d H:i:s'), 'type' => $order['type'], 'aid' => $order['aid']]);
                    //减少库存
                    (new GoodsSpec())->where(['id' => $goods['specid'], 'goodid' => $goods['goodsid']])->setDec('goodsstock', $goods['number']);
                    //增加销量
                    (new Goods())->where(['id' => $goods['goodsid']])->setInc('sale', $goods['number']);
                }
                $shop = (new Shoplist())->where('id', $order['shopid'])->find();
                //更新商家累计营业额
                if ($shop['date'] < date('Y-m-d')) {
                    $todaytotal = $order['total'];
                } else {
                    $todaytotal = $shop['todaytotal'] + $order['total'];
                }
                (new Shoplist())->where('id', $order['shopid'])->update(['total' => ($shop['total'] + $order['total']), 'date' => date('Y-m-d H:i:s'), 'todaytotal' => $todaytotal]);

                if ($order['type'] == 1 && $order['payment'] == 2) {
                    (new ShopLog())->MoneyEdit($shop, $order['total'], 1, '订单完成', $order['id']);
                }

                if ($order['type'] == 2 && $order['payment'] == 2) {
                    $admin = (new Admin())->where('id', $order['aid'])->find();
                    (new Admin())->MoneyEdit($admin, $order['total'], 1, '订单完成', $order['id']);
                }

                //用户进行上架绑定
                $user = (new User())->where('id', $order['user_id'])->find();
                if (!empty($user) && $user['shopid'] == 0) {
                    (new User())->where('id', $order['user_id'])->update(['shopid' => $order['shopid']]);
                }
                break;
                //取消操作
            case '5':
                if ($order['status'] == 4 || $order['status'] == 5) {
                    throw new \Exception("订单状态不可更改", 100);
                }
                //订单取消
                $this->where('id', $order['id'])->update(['status' => $status, 'caceltime' => date('Y-m-d H:i:s')]);
                //线上支付退款
                if ($order['payment'] == 2 && $order['ispay'] == 2) {
                }
                break;
            default:
                # code...
                break;
        }
    }
}
